import { Module } from '@nestjs/common';
import { DemoService } from './demo.service';
import { DemoController } from './demo.controller';
import { Demo2Service } from './demo2.service';
import { Demo3Service } from './demo3.service';

@Module({
  controllers: [DemoController], //在Controller 就可以使用注入好的service 了
  // providers: [DemoService], //Provider 只是一个用 @Injectable() 装饰器注释的类。
  /* 就是一个语法糖
  1 {
    provide: "xxx",
    useClass: DemoService
  } 
  2 自定义名称之后 controllers 需要用对应的Inject 取 不然会找不到的
    @Iniect('xxx') private readonly userService:UserService
  */
  providers: [
    {
      provide: 'DemoSyntacticSugar',
      useClass: DemoService,
    },
    {
      provide: 'Constant',
      useValue: ['MB', 'PXX', 'JD'],
    },
    Demo2Service,
    {
      provide: 'Test',
      inject: [Demo2Service],
      useFactory(Demo2Service: Demo2Service) {
        //工厂模式
        return new Demo3Service(Demo2Service);
      },
    },
    {
      provide: 'Sync',
      async useFactory() {
        //异步
        return await new Promise((r) => {
          setTimeout(() => {
            r('sync');
          }, 3000);
        });
      },
    },
  ],
})
export class DemoModule {}
